﻿using System;
using System.Xml;
using System.IO;

namespace HBASite
{
    
    static public class AdminLog
    {
        
        static public string DEFAULT_USER = "Anonymous";
        static public string CURR_USER = DEFAULT_USER;
        static public string COUNTER_USER = "CounterDownloads";
        static public int DEFAULT_COUNTER = 0;
        static public void AdminLogSet(string strTime, string strUser, string strAction)
        {
            string strAdminFile = @"c:\AdminView.xml";
            string strRecordTag = "LogRecord";
            string strTimeTag = "Time";
            string strUserTag = "User";
            string strActionTag = "Action";

            // Create the XmlDocument.
            XmlDocument doc = new XmlDocument();
            bool bFileExist = true;
            if (System.IO.File.Exists(strAdminFile))
            {
                doc.Load(strAdminFile);
            }
            else
            {
                bFileExist = false;
            }

            // Add a price element.
            XmlElement newElem = doc.CreateElement(strRecordTag);
            XmlElement newElemTime = doc.CreateElement(strTimeTag);
            XmlElement newElemUser = doc.CreateElement(strUserTag);
            XmlElement newElemAction = doc.CreateElement(strActionTag);
            
            newElemTime.InnerText = "Time: " + strTime;
            newElemUser.InnerText = "User: " + strUser;
            newElemAction.InnerText = "Actions: " + strAction;

            newElem.AppendChild(newElemTime);
            newElem.AppendChild(newElemUser);
            newElem.AppendChild(newElemAction);

            //newElem.InnerText = "10.95";
            if (bFileExist)
            {
                doc.DocumentElement.AppendChild(newElem);
            }
            else
            {
                doc.AppendChild(newElem);
            }

            // Save the document to a file and auto-indent the output.
            XmlTextWriter writer = new XmlTextWriter(strAdminFile, null);
            doc.Save(writer);
            writer.Close();
        }
        static public void SetCounter()
        {
            string strAdminFile = @"c:\Counter.xml";
            string strRecordTag = "DownloadCounter";

            // Create the XmlDocument.
            XmlDocument doc = new XmlDocument();
            bool bFileExist = true;
            int nCurrCount = DEFAULT_COUNTER;
            if (System.IO.File.Exists(strAdminFile))
            {
                doc.Load(strAdminFile);
                nCurrCount = int.Parse(doc.InnerText);
            }
            else
            {
                nCurrCount = DEFAULT_COUNTER;
                bFileExist = false;
            }

            nCurrCount++;
            if (bFileExist)
            {
                doc.DocumentElement.InnerText = nCurrCount.ToString();
            }
            else
            {
                // Add a price element.
                XmlElement newElem = doc.CreateElement(strRecordTag);

                newElem.InnerText = nCurrCount.ToString();

                //newElem.AppendChild(newElem);
                doc.AppendChild(newElem);
            }
            
            // Save the document to a file and auto-indent the output.
            XmlTextWriter writer = new XmlTextWriter(strAdminFile, null);
            doc.Save(writer);
            writer.Close();
        }
    }
}